﻿using Domain.Process_Procedures;
using Domain.User;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Process_Procedures;
using MES.Server.API.Dto.ProcessProceduresDTO;
using Microsoft.EntityFrameworkCore;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Process_Procedures.ProcessManagement
{
    public class GetProcessManagementHandler : IRequestHandler<GetProcessManagementCommand, ApiPaging<ProcessManagementDTO>>
    {
        private readonly IRepository<ProcessManagementModel> processmgRepo;
        private readonly IRepository<AccountModel> accRepo;
        private readonly IRepository<DepartmentModel> depRepo;
        private readonly IRepository<ProcessCategoryModel> processCatRepo;
        /// <summary>
        /// 注入所需仓储
        /// </summary>
        /// <param name="ProcessmgRepo">流程管理仓储接口</param>
        /// <param name="accRepo">账户仓储接口</param>
        /// <param name="DepRepo">部门仓储接口</param>
        /// <param name="ProcessCatRepo">流程分类仓储接口</param>

        public GetProcessManagementHandler(IRepository<ProcessManagementModel> ProcessmgRepo, IRepository<AccountModel> accRepo,
            IRepository<DepartmentModel> DepRepo,IRepository<ProcessCategoryModel> ProcessCatRepo)
        {
            processmgRepo = ProcessmgRepo;
            this.accRepo = accRepo;
            depRepo = DepRepo;
            processCatRepo = ProcessCatRepo;
        }
        public async Task<ApiPaging<ProcessManagementDTO>> Handle(GetProcessManagementCommand request, CancellationToken cancellationToken)
        {

            var processmg = processmgRepo.GetAll().Where(x=>!x.IsDel);
            var processCat = processCatRepo.GetAll().Where(x => !x.IsDel);
            var account = accRepo.GetAll().Where(x => !x.IsDel);
            var department = depRepo.GetAll().Where(x => !x.IsDel);
            //工序编号
            if (!string.IsNullOrEmpty(request.ProcessCode))
            {
                 processmg = processmg.Where(x => x.ProcessCode.Contains(request.ProcessCode));
            }
            //工序名称
            if (!string.IsNullOrEmpty(request.ProcessName))
            {
                 processmg = processmg.Where(x => x.ProcessName.Contains(request.ProcessName));
            }
            //工序类别
            if (request.CategoryId!= null)
            {
                processCat = processCat.Where(x => x.Id == request.CategoryId);
            }
            //负责人
            if (request.PersonId != null)
            {
                account = account.Where(x => x.Id == request.PersonId);
            }
            //所属部门
            if (request.DepartmentId != null)
            {
                department = department.Where(x => x.Id == request.DepartmentId);
            }
            //状态
            if (request.IsActive != null)
            {
                processmg = processmg.Where(x => x.IsActive == request.IsActive);
            }
            var linq = from a in processmg
                       join b in processCat on a.CategoryId equals b.Id
                       join c in account on a.PersonId equals c.Id
                       join d in department on a.DepartmentId equals d.Id
                       select new ProcessManagementDTO()
                       {
                           Id= a.Id,
                           ProcessCode = a.ProcessCode,
                           ProcessName = a.ProcessName,
                           CategoryId = a.CategoryId,
                           categoryName = b.CategoryName,
                           PersonId = a.PersonId,
                           PersonName= c.RealName,
                           DepartmentId = a.DepartmentId,
                           DepartmentName=d.DepartmentName,
                           IsActive = a.IsActive,
                           Remarks=a.Remarks,
                       };
            var res = new ApiPaging<ProcessManagementDTO>();
            res.Totalcount= await linq.CountAsync();
            res.TotalPage = await linq.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToListAsync();
            res.Message = "查询成功";
            res.Code = StatusCode.Success;
            return res;
        }
    }
}
